<?php

namespace app\admin\controller;

use app\common\controller\Admin;

/**
 * 用户资金
 * Class Money
 * @package app\admin\controller
 */
class Money extends Admin
{

    /**
     * 资金日志
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $param = $this->request->param();

        $map = [];

//		$map['status'] = array('egt', 0);
//		if (isset($param['nickname']) && $param['nickname']) {
//			$map['nickname'] = array('like', '%' . $param['nickname'] . '%');
//		}
//		if (isset($param['username']) && $param['username']) {
//			$map['username'] = array('like', '%' . (string) $param['nickname'] . '%');
//		}

        if (isset($param['uid']) && $param['uid'] > 0) {
            $map['uid'] = intval($param['uid']);
        }

        $list = model('UserMoneyLog')->where($map)->order('id desc')
            ->paginate(15, false, array(
                'param' => $param
            ));

        $data = array(
            'list' => $list,
            'page' => $list->render(),
            'param' => $param
        );
        $this->assign($data);
        $this->setMeta('用户资金流水列表');
        return $this->fetch();
    }

    /**
     * 变更账户金额
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function change()
    {
        if ($this->request->isPost()) {

            $uid = $this->request->post('uid', 0, 'intval');
            $info = $this->request->post('info');
            if ($uid <= 0) $this->error('变更哪个客户必选！');

            $done = false;

            $account_money = $this->request->post('account_money', 0);
            $money_freeze = $this->request->post('money_freeze', 0);
            $money_collect = $this->request->post('money_collect', 0);

            //处理账户金额
            if (floatval($account_money) != 0) {
                $done = \app\common\serve\User::memberMoneyLog($uid, 71, $account_money, $info, 0);
            }

            //处理冻结金额
            if (floatval($money_freeze) != 0) {
                $done = \app\common\serve\User::memberMoneyLog($uid, 72, $money_freeze, $info, 0);
            }

            //处理待收金额
            if (floatval($money_collect) != 0) {
                $done = \app\common\serve\User::memberMoneyLog($uid, 73, $money_collect, $info, 0);
            }

            if ($done) {
                $this->success('变更金额成功！');
            } else {
                $this->error('变更金额失败！');
            }
        }

        $this->redirect('/');
    }

}